set more off, perm
set maxvar 120000
set varabbrev off
* ---------------------------------------------- *
global dir 	"`1'"
global Data 	$dir/Data
global Tables 	$dir/Tables
global Figures 	$dir/Figures
global Work 	$dir/Work
global Temp 	$dir/Temp
global Pseudo	$dir/Work/Temp
* ---------------------------------------------- *
import sas using $dir/Data/CRSP/msedelist.sas7bdat, clear
rename *, lower
drop issuno compno hexcd hsiccd

format dlstdt nextdt dlpdt %td

* get rid of firms that died before the 1st month with LTG data
keep if dlstdt>mdy(12,1,1981)
gen dlstmo=mofd(dlstdt)
format dlstmo %tm

* dlstcd: 1st digit
*1	still trading or halted but not yet delisted
*2	mergers
*3	exchanges
*4	liquidations
*5	delisted by NYSE, NYSE American, NASDAQ, or Arca
*7	delisted by the Securities and Exchange Commission
*8	trading simultaneously on more than one exchange
save $Temp/delist.dta, replace
* ********************************************************************* */
* Get data on market cap and returns; compute vw return and mcap of the firm
* Keep the permno of the security with the largest mmap
* msf_adjusted.dta generated by read_crsp.do
use $Work/msf_adjusted, clear

* Create weight to compute vw return and collapse data at the firm level
* deal with obs with missing mcap data (mostly IPOs)
tsset permno datem
 
generate Lmcap=L.mcap 		if L.mcap>0
replace  Lmcap=mcap/(1+retx) 	if Lmcap==. & mcap>0

keep if ret+Lmcap<.
/* ********************************************************************* */
* figure out how many firms have multiple classes of (common) shares
keep if shrcd==10 | shrcd==11
keep if exchcd>=1 & exchcd<=3
duplicates tag permco datem, gen(dup)
tabulate dup
***************************
* when firm has multiple securities, keep data for security with the largest market cap
sort permco datem mcap
by permco datem: gen obs=_n
by permco datem: gen tot=_N
keep if obs==tot
keep comnam permco permno datem siccd shrcd exchcd date mcap shrout prc retx ret
***************************
tsset permco datem
forvalues x=1(1)60 {
	quietly: generate  ret`x'=F`x'.ret 
	quietly: generate retx`x'=F`x'.retx 
}
order *, sequential
keep if exchcd>0  & exchcd<4
keep  comnam permco permno siccd exchcd datem date mcap shrout prc ret retx ret1-ret60 retx1-retx60
order comnam permco permno siccd exchcd datem date mcap shrout prc ret retx ret1-ret60 retx1-retx60
* *****************************************************
merge m:1 permco permno using $Temp/delist.dta
drop if _merge==2
drop _merge
* *****************************************************
* merge with IBES data from read_ibes.do (drops multiple listings)
keep if dofm(datem)>=mdy(12,1,1981)
merge 1:1 permco permno datem using $Work/XSibes.dta
gen SMPL_IBES=(_merge==3)
drop if _merge==2
drop _merge

foreach var of varlist LTG* {
	replace `var'=`var'/100
}
bys   datem: astile RNK=LTG		, nq(10)
* *****************************************************
forvalues x=1(1)5 {
	quietly: generate  ret_yr`x'=1
	quietly: generate retx_yr`x'=1
}
forvalues x=1(1)12 {
	quietly: replace  ret_yr1= ret_yr1*(1+ ret`x')  
	quietly: replace retx_yr1=retx_yr1*(1+retx`x')  
}
forvalues x=13(1)24 {
	quietly: replace  ret_yr2= ret_yr2*(1+ ret`x')	 
	quietly: replace retx_yr2=retx_yr2*(1+retx`x')	  
}
forvalues x=25(1)36 {
	quietly: replace  ret_yr3= ret_yr3*(1+ ret`x')	 
	quietly: replace retx_yr3=retx_yr3*(1+retx`x')	  
}
forvalues x=37(1)48 {
	quietly: replace  ret_yr4= ret_yr4*(1+ ret`x')	 
	quietly: replace retx_yr4=retx_yr4*(1+retx`x')	 
}
forvalues x=49(1)60 {
	quietly: replace  ret_yr5= ret_yr5*(1+ ret`x')	 
	quietly: replace retx_yr5=retx_yr5*(1+retx`x')	 
}
* *****************************************************
replace prc=abs(prc)
save $Work/ibes_ret, replace

!find $Temp -type f -delete
